/*
 * $Id: MavenArtifactResolver.java 70 2010-11-21 03:01:14Z samaxes $
 *
 * Copyright 2010 samaxes.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.samaxes.stripes.test;

import java.io.File;

/**
 * Resolver that converts a Maven artifact reference into a {@link File} object (also supports a single argument
 * shorthand notation that can resolve one or more artifacts).
 * 
 * <p>
 * If you are using an alternative location for your local Maven repository, controlled using the {@code
 * maven.repo.local} Maven property, you will need to promote that property as a Java system property in the surefire
 * plugin configuration:
 * </p>
 * 
 * <pre>
 * {@code
 * <plugin>       
 *    <artifactId>maven-surefire-plugin</artifactId>
 *    <version>2.6</version>
 *    <configuration>
 *       <systemProperties>
 *          <property>
 *             <name>maven.repo.local</name>
 *             <value>$}{maven.repo.local}{@code</value>
 *          </property>
 *       </systemProperties>
 *    </configuration>
 * </plugin>
 * }
 * </pre>
 * 
 * <p>
 * Otherwise, the default location will be used.
 * </p>
 * 
 * @author Samuel Santos
 * @version $Revision$
 */
public class MavenArtifactResolver {

    private static final String LOCAL_MAVEN_REPO = System.getProperty("maven.repo.local") != null ? System
            .getProperty("maven.repo.local") : (System.getProperty("user.home") + File.separatorChar + ".m2"
            + File.separatorChar + "repository");

    public static File resolve(final String groupId, final String artifactId, final String version) {
        return resolve(groupId, artifactId, version, null);
    }

    public static File resolve(final String groupId, final String artifactId, final String version,
            final String classifier) {
        return new File(LOCAL_MAVEN_REPO + File.separatorChar + groupId.replace(".", File.separator)
                + File.separatorChar + artifactId + File.separatorChar + version + File.separatorChar + artifactId
                + "-" + version + (classifier != null ? ("-" + classifier) : "") + ".jar");
    }

    public static File resolve(final String qualifiedArtifactId) {
        String[] segments = qualifiedArtifactId.split(":");
        if (segments.length == 3) {
            return resolve(segments[0], segments[1], segments[2]);
        } else if (segments.length == 4) {
            return resolve(segments[0], segments[1], segments[2], segments[3]);
        }
        throw new IllegalArgumentException("Invalid qualified artifactId syntax: " + qualifiedArtifactId);
    }

    public static File[] resolve(final String... qualifiedArtifactIds) {
        int n = qualifiedArtifactIds.length;
        File[] artifacts = new File[n];
        for (int i = 0; i < n; i++) {
            artifacts[i] = resolve(qualifiedArtifactIds[i]);
        }

        return artifacts;
    }
}
